﻿using FDSQLAssistant.Extensions;
using FDSQLAssistant.Models;
using FDSQLAssistant.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FDSQLAssistant.Services
{
    public class ModelGenerator : IGenerator
    {
        private readonly ICommonRepository _commonRepository;

        public ModelGenerator(IEnumerable<ICommonRepository> commonRepositories, Config config)
        {
            _commonRepository = commonRepositories.First(x => x.DbType() == config.DbType);
            if (_commonRepository == null)
                throw new ArgumentNullException($"配置项有误，不存在对应连接串{config.DbType}.{config.Name}-{config.Order}");
        }

        public string DeleteSql(string tableName, string condition)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("DELETE FROM {0}", tableName);
            if (!condition.IsNullOrEmpty())
            {
                sb.AppendFormat(" WHERE {0}", condition);
            }

            return sb.ToString();
        }

        public string InsertSql(string tableName)
        {
            var ti = _commonRepository.GetTableInfo(tableName);
            if (ti == null || ti.ColumnInfos.Count == 0)
                return string.Empty;
            StringBuilder sb = new StringBuilder();
            StringBuilder sbV = new StringBuilder();
            sb.AppendFormat("INSERT INTO {0}(", tableName);
            foreach (var colInfo in ti.ColumnInfos)
            {
                if (colInfo.IsIdentity)
                    continue;
                sb.AppendFormat("{0},", colInfo.Name);
                sbV.AppendFormat("@{0},", colInfo.Name);
            }
            sb.Remove(sb.Length - 1, 1);
            sbV.Remove(sbV.Length - 1, 1);
            sb.Append(") VALUES (");
            sb.Append(sbV.ToString());
            sb.Append(")");
            return sb.ToString();
        }

        public string QuerySql(string tableName, string condition)
        {
            var ti = _commonRepository.GetTableInfo(tableName);
            if (ti == null || ti.ColumnInfos.Count == 0)
                return string.Empty;
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT ");
            foreach (var colInfo in ti.ColumnInfos)
            {
                sb.AppendFormat("{0},", colInfo.Name);
            }

            sb.Remove(sb.Length - 1, 1);
            sb.AppendFormat(" FROM {0}", tableName);
            if (!condition.IsNullOrEmpty())
            {
                sb.AppendFormat(" WHERE {0}", condition);
            }

            return sb.ToString();
        }

        public string UpdateSql(string tableName, string condition)
        {
            var ti = _commonRepository.GetTableInfo(tableName);
            if (ti == null || ti.ColumnInfos.Count == 0)
                return string.Empty;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("UPDATE {0} SET ", tableName);
            foreach (var colInfo in ti.ColumnInfos)
            {
                if (colInfo.IsPk)
                    continue;
                sb.AppendFormat("{0}=@{0},", colInfo.Name);
            }
            sb.Remove(sb.Length - 1, 1);
            if (!condition.IsNullOrEmpty())
            {
                sb.AppendFormat(" WHERE {0}", condition);
            }
            return sb.ToString();
        }
    }
}
